home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / modules / wrap.js < prev   
Text File  |  2008-07-30  |  7KB  |  212 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Dan Mills <thunder@mozilla.com>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. const EXPORTED_SYMBOLS = ['Wrap'];
  38.  
  39. const Cc = Components.classes;
  40. const Ci = Components.interfaces;
  41. const Cr = Components.results;
  42. const Cu = Components.utils;
  43.  
  44. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  45. Cu.import("resource://weave/log4moz.js");
  46. Cu.import("resource://weave/dav.js");
  47. Cu.import("resource://weave/async.js");
  48. Cu.import("resource://weave/faultTolerance.js");
  49.  
  50. Function.prototype.async = Async.sugar;
  51.  
  52. /*
  53.  * Wrapper utility functions
  54.  *
  55.  * Not in util.js because that would cause a circular dependency
  56.  * between util.js and async.js (we include async.js so that our
  57.  * returned generator functions have the .async sugar defined)
  58.  */
  59.  
  60. let Wrap = {
  61.  
  62.   // NOTE: requires _osPrefix string property in your object
  63.   // NOTE2: copy this function over to your objects, use like this:
  64.   //
  65.   // MyObj.prototype = {
  66.   //   _notify: Wrap.notify,
  67.   //   ...
  68.   //   method: function MyMethod() {
  69.   //     let self = yield;
  70.   //     ...
  71.   //     // doFoo is assumed to be an asynchronous method
  72.   //     this._notify("foo", this._doFoo, arg1, arg2).async(this, self.cb);
  73.   //     let ret = yield;
  74.   //     ...
  75.   //   }
  76.   // };
  77.   notify: function Weave_notify(name, payload, method /* , arg1, arg2, ..., argN */) {
  78.     let savedName = name;
  79.     let savedPayload = payload;
  80.     let savedMethod = method;
  81.     let savedArgs = Array.prototype.slice.call(arguments, 3);
  82.  
  83.     return function WeaveNotifyWrapper(/* argN+1, argN+2, ... */) {
  84.       let self = yield;
  85.       let ret;
  86.       let args = Array.prototype.slice.call(arguments);
  87.  
  88.       try {
  89.         this._os.notifyObservers(null, this._osPrefix + savedName + ":start", savedPayload);
  90.         this._os.notifyObservers(null, this._osPrefix + "global:start", savedPayload);
  91.  
  92.         args = savedArgs.concat(args);
  93.         args.unshift(this, savedMethod, self.cb);
  94.         Async.run.apply(Async, args);
  95.         ret = yield;
  96.  
  97.         this._os.notifyObservers(null, this._osPrefix + savedName + ":success", savedPayload);
  98.         this._os.notifyObservers(null, this._osPrefix + "global:success", savedPayload);
  99.  
  100.       } catch (e) {
  101.         this._os.notifyObservers(null, this._osPrefix + savedName + ":error", savedPayload);
  102.         this._os.notifyObservers(null, this._osPrefix + "global:error", savedPayload);
  103.         if (e != "Could not acquire lock") // FIXME HACK
  104.           throw e;
  105.       }
  106.  
  107.       self.done(ret);
  108.     };
  109.   },
  110.  
  111.   // NOTE: see notify, this works the same way.  they can be
  112.   // chained together as well.
  113.   lock: function WeaveSync_lock(method /* , arg1, arg2, ..., argN */) {
  114.     let savedMethod = method;
  115.     let savedArgs = Array.prototype.slice.call(arguments, 1);
  116.  
  117.     return function WeaveLockWrapper( /* argN+1, argN+2, ... */) {
  118.       let self = yield;
  119.       let ret;
  120.       let args = Array.prototype.slice.call(arguments);
  121.  
  122.       if (!this._loggedIn)
  123.         throw "Could not acquire lock (not logged in)";
  124.       if (DAV.locked)
  125.         throw "Could not acquire lock (lock already held)";
  126.  
  127.       let locked = yield DAV.lock.async(DAV, self.cb);
  128.       if (!locked)
  129.         throw "Could not acquire lock";
  130.  
  131.       this._os.notifyObservers(null, this._osPrefix + "lock:acquired", "");
  132.  
  133.       try {
  134.         args = savedArgs.concat(args);
  135.         args.unshift(this, savedMethod, self.cb);
  136.         ret = yield Async.run.apply(Async, args);
  137.  
  138.       } catch (e) {
  139.         throw e;
  140.  
  141.       } finally {
  142.         yield DAV.unlock.async(DAV, self.cb);
  143.         this._os.notifyObservers(null, this._osPrefix + "lock:released", "");
  144.       }
  145.  
  146.       self.done(ret);
  147.     };
  148.   },
  149.  
  150.   // NOTE: see notify, this works the same way.  they can be
  151.   // chained together as well.
  152.   localLock: function WeaveSync_localLock(method /* , arg1, arg2, ..., argN */) {
  153.     let savedMethod = method;
  154.     let savedArgs = Array.prototype.slice.call(arguments, 1);
  155.  
  156.     return function WeaveLocalLockWrapper(/* argN+1, argN+2, ... */) {
  157.       let self = yield;
  158.       let ret;
  159.       let args = Array.prototype.slice.call(arguments);
  160.  
  161.       if (DAV.locked)
  162.         throw "Could not acquire lock";
  163.       DAV.allowLock = false;
  164.  
  165.       this._os.notifyObservers(null,
  166.                                this._osPrefix + "local-lock:acquired", "");
  167.  
  168.       try {
  169.         args = savedArgs.concat(args);
  170.         args.unshift(this, savedMethod, self.cb);
  171.         ret = yield Async.run.apply(Async, args);
  172.  
  173.       } catch (e) {
  174.         throw e;
  175.  
  176.       } finally {
  177.         DAV.allowLock = true;
  178.         this._os.notifyObservers(null,
  179.                                  this._osPrefix + "local-lock:released", "");
  180.       }
  181.  
  182.       self.done(ret);
  183.     };
  184.   },
  185.  
  186.   // NOTE: see notify, this works the same way.  they can be
  187.   // chained together as well.
  188.   // catchAll catches any exceptions and passes them to the fault tolerance svc
  189.   catchAll: function WeaveSync_catchAll(method /* , arg1, arg2, ..., argN */) {
  190.     let savedMethod = method;
  191.     let savedArgs = Array.prototype.slice.call(arguments, 1);
  192.  
  193.     return function WeaveCatchAllWrapper(/* argN+1, argN+2, ... */) {
  194.       let self = yield;
  195.       let ret;
  196.       let args = Array.prototype.slice.call(arguments);
  197.  
  198.       try {
  199.         args = savedArgs.concat(args);
  200.         args.unshift(this, savedMethod, self.cb);
  201.         ret = yield Async.run.apply(Async, args);
  202.  
  203.       } catch (e) {
  204.         ret = FaultTolerance.Service.onException(e);
  205.         if (!ret)
  206.           throw e;
  207.       }
  208.       self.done(ret);
  209.     };
  210.   }
  211. }
  212.